﻿using Microsoft.Phone.Controls;
using System;
using System.Windows.Navigation;

namespace LimitlessLightSwitch
{
  /// <summary>
  /// Useful extenders (yay!)
  /// </summary>
  public static class Extender
  {
    private static object Data; //Data for the navigate methods

    /// <summary>
    /// Converts to int16, returns 0 if conversion fails.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static Int16 ToInt16(this object value)
    {
      Int16 result = 0;

      String StringValue = value.ToString();

      if (!string.IsNullOrEmpty(StringValue))
        Int16.TryParse(StringValue, out result);

      return result;
    }

    /// <summary>
    /// Converts to int32, returns 0 if conversion fails.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static Int32 ToInt32(this object value)
    {
      Int32 result = 0;
      String StringValue = value.ToString();
      if (!string.IsNullOrEmpty(StringValue))
        Int32.TryParse(StringValue, out result);

      return result;
    }

    /// <summary>
    /// Converts to int64, returns 0 if conversion fails.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static Int64 ToInt64(this object value)
    {
      Int64 result = 0;
      String StringValue = value.ToString();
      if (!string.IsNullOrEmpty(StringValue))
        Int64.TryParse(StringValue, out result);

      return result;
    }

    /// <summary>
    /// Navigates to the content specified by uniform resource identifier (URI).
    /// </summary>
    /// <param name="navigationService">The navigation service.</param>
    /// <param name="source">The URI of the content to navigate to.</param>
    /// <param name="data">The data that you need to pass to the other page 
    /// specified in URI.</param>
    public static void Navigate(this NavigationService navigationService, Uri source, object data)
    {
      Data = data;
      navigationService.Navigate(source);
    }

    /// <summary>
    /// Navigates to the specified frame.
    /// </summary>
    /// <param name="Frame">The frame.</param>
    /// <param name="source">The source.</param>
    /// <param name="data">The data.</param>
    public static void Navigate(this PhoneApplicationFrame Frame, Uri source, object data)
    {
      Data = data;
      Frame.Navigate(source);
    }

    /// <summary>
    /// Gets the navigation data passed from the previous page.
    /// </summary>
    /// <param name="service">The service.</param>
    /// <returns>System.Object.</returns>
    public static object GetNavigationData(this NavigationService service)
    {
      return Data;
    }


  }
}
